package com.edward.service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ExistentInvoice;
import com.edward.model.UnDb;
import com.edward.model.UnSubmit;

public class UnDbManager extends HibernateDaoSupport {

	public void addUnDb(UnDb unDb) {
		
		getHibernateTemplate().save(unDb);
	}
	
	public void updateUnDb(UnDb unDb) {
		
		getHibernateTemplate().update(unDb);
	}
	
	public void deleteUnDb(int id){
		
		getHibernateTemplate().delete(getUnDb(id));
	}
	
	public UnDb getUnDb(int id){
		UnDb d =(UnDb)getHibernateTemplate().get(UnDb.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<UnDb> listUnDb() {
		return getHibernateTemplate().find("from UnDb u where u.tag = 0");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <UnDb>list = getHibernateTemplate().find("from UnDb u where u.tag = 0" );
		UnDb u = new UnDb();
		u.setCode("合计");
		for (UnDb unDb : list) {
			u.setTaxSum(unDb.getTaxSum() + u.getTaxSum());
		}
		int a = 0;
		double d ;
		a = (int) (u.getTaxSum() * 100);
		d = a;
		u.setTaxSum(d/100);
		list.add(u);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <UnDb>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	public void addDbItem(String code,Date submitDate,int id){
		UnDb ud = getUnDb(id);
		List<UnSubmit> l = getHibernateTemplate().find("from UnSubmit u where u.checkCode = '" + code + "'" );
		l.get(0).setTag(1);
		List <ExistentInvoice> list =  getEIList(l.get(0).getId()); 
		for(ExistentInvoice ei : list){
			ei.setSubmitDate(submitDate);
			ud.setTaxSum(ud.getTaxSum() + ei.getTax());
			getHibernateTemplate().update(ei);
			if(ud.getItems() == null){
				ud.setItems(new ArrayList<ExistentInvoice>());
			}
			ud.getItems().add(ei);
		}
		getHibernateTemplate().update(ud);
		getHibernateTemplate().update(l.get(0));
		
	}
		
		public void addDbItems(UnDb unDb,int id){
			UnDb  ud = addcheck(unDb);
			UnSubmit unSubmit = (UnSubmit)getHibernateTemplate().get(UnSubmit.class, id);
			unSubmit.setTag(1);
			List <ExistentInvoice> list =  getEIList(id); 
			for(ExistentInvoice ei : list){
				ei.setSubmitDate(new Date());
				ud.setTaxSum(ud.getTaxSum() + ei.getTax());
				getHibernateTemplate().update(ei);
				if(ud.getItems() == null){
					ud.setItems(new ArrayList<ExistentInvoice>());
				}
				ud.getItems().add(ei);
			}
			getHibernateTemplate().update(ud);
			getHibernateTemplate().update(unSubmit);
	}
		
		@SuppressWarnings("unchecked")
		public void updateItem(String code ,Date submitDate){
			List<UnSubmit> l = getHibernateTemplate().find("from UnSubmit u where u.checkCode = '" + code + "'" );
			List <ExistentInvoice> list =  getEIList(l.get(0).getId()); 
			for(ExistentInvoice ei : list){
				ei.setSubmitDate(submitDate);
				getHibernateTemplate().update(ei);
			}
			
		}
		
		
		@SuppressWarnings("unchecked")
		public void deleteItem(int id ,String code){
			UnDb ud = (UnDb)getHibernateTemplate().get(UnDb.class, id);
			List<UnSubmit> l = getHibernateTemplate().find("from UnSubmit u where u.checkCode = '" + code + "'" );
			List <ExistentInvoice> list =  getEIList(l.get(0).getId()); 
			for(ExistentInvoice ei : list){
				ud.getItems().remove(ei);
				ud.setTaxSum(ud.getTaxSum() - ei.getTax());
				ei.setSubmitDate(null);
				getHibernateTemplate().update(ei);
			}
			l.get(0).setTag(0);
			getHibernateTemplate().update(ud);
			getHibernateTemplate().update(l.get(0));
			
		}
		
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerDbItem(int start,int limit,String code) {
		List<UnSubmit> l = getHibernateTemplate().find("from UnSubmit u where u.checkCode = '" + code + "'" );
		List <ExistentInvoice> list = getEIList(l.get(0).getId()); 
			ExistentInvoice e = new ExistentInvoice();
			UnSubmit u = new UnSubmit();
			u.setCheckCode("合计");
			e.setUnSubmit(u);
			for (ExistentInvoice existentInvoice : list) {
				e.setMoney(e.getMoney() + existentInvoice.getMoney());
				e.setTax(e.getTax() + existentInvoice.getTax());
			}
			int a = 0;
			double d ;
			a = (int) (e.getMoney() * 100);
			d = a;
			e.setMoney(d/100);
			a = (int) (e.getTax() * 100);
			d = a;
			e.setTax(d/100);
			list.add(e);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ExistentInvoice>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerDbItem(int start,int limit,int id) {
		List <ExistentInvoice>list = getHibernateTemplate().find("from ExistentInvoice e where DRAWBACK_ID = " + id );
		ExistentInvoice e = new ExistentInvoice();
		UnSubmit u = new UnSubmit();
		u.setCheckCode("合计");
		e.setUnSubmit(u);
		for (ExistentInvoice existentInvoice : list) {
			e.setMoney(e.getMoney() + existentInvoice.getMoney());
			e.setTax(e.getTax() + existentInvoice.getTax());
		}
		int a = 0;
		double d ;
		a = (int) (e.getMoney() * 100);
		d = a;
		e.setMoney(d/100);
		a = (int) (e.getTax() * 100);
		d = a;
		e.setTax(d/100);
		list.add(e);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ExistentInvoice>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	
	@SuppressWarnings("unchecked")
	public List<ExistentInvoice> getEIList(int id){
		List <ExistentInvoice>list = getHibernateTemplate().find("from ExistentInvoice e where SUBMIT_ID = " + id );
		return list;
		
	}
	
	
	@SuppressWarnings("unchecked")
	public UnDb addcheck(UnDb unDb){
		List<UnDb> l = getHibernateTemplate().find("from UnDb u where u.code = '" + unDb.getCode() + "'" );
		if(l.size() == 0){
			getHibernateTemplate().save(unDb);
			return unDb;
		}else{
			return l.get(0);
		}
		
	}




}



